package com.fingard.dsp.bank.directbank.hbc01;

import java.io.File;
import java.util.Date;

import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.directbank.hsbc01.EnumTransMode;
import com.fingard.dsp.bank.directbank.hsbc01.HSBC01Base;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.io.FileHelper;
import com.fingard.net.ConnState;
import com.fingard.net.FtpReceiver;

/**
 * @author XueJy on 2018/9/19.
 * @version 1.0
 */
public class HBC01BatPayRec extends HBC01Base implements IBatPayRec {

    @Override
    public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req1988.reqHeader);
        //returnAts报文头
        retAts.retHeader.bankCode = p_req1988.reqHeader.bankCode;
        retAts.retHeader.transType = p_req1988.reqHeader.transType;
        retAts.retHeader.transSeqID = p_req1988.reqHeader.transSeqID;
        //支付请求码
        retAts.reqSeqID = p_req1988.reqBatchNO;
        //指令代码
        StringBuilder tmpMsg = new StringBuilder();
        writePayCommonHeader(tmpMsg, p_req1988);
        writePayBodyParams(tmpMsg, p_req1988);

        String path = getCommonDataFileDirectory(new Date(), "HSHBC") + File.separator + "batchPay";
        String filename = p_req1988.reqBatchNO + ".txt";
        String filePath = path + File.separator + filename;
        FileHelper.writeFile(filePath, tmpMsg.toString(), getCharset());

        HSBC01Base hsbc01Base = new HSBC01Base();
        hsbc01Base.bankFront = this.bankFront;
        hsbc01Base.reqHeader = this.reqHeader;
        hsbc01Base.sbLog = this.sbLog;
   //     FtpReceiver ftpReceiver = sendToBank(path, path);
        FtpReceiver ftpReceiver = hsbc01Base.sendToBank(path, filename, EnumTransMode.BatchPayForHS.getValue());
        retAts.respCode = "0";
        if (ftpReceiver.isCompleted) {
            retAts.respInfo = "支付文件上传成功";
            retAts.transState = "Uploaded";
            retAts.payInfoCode = "Uploaded";
        } else if (ConnState.FAIL.equals(ftpReceiver.connState)) {
            retAts.respInfo = "支付文件上传失败";
            retAts.transState = "BatchFailed";
            retAts.payInfoCode = "BatchFailed";
        } else {
            retAts.respCode = "-1";
            retAts.respInfo = ftpReceiver.connMsg;
        }

        return retAts;
    }



    @Override
    public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
        return null;
    }

    @Override
    public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
        return null;
    }

    @Override
    public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
        return null;
    }
}
